package com.xin.dao.impl;

import com.xin.dao.BookDao;
import com.xin.pojo.Book;

import java.util.List;

public class BookDaoimpl extends BaseDao implements BookDao {
    @Override
    public int addBook(Book book) {
        String sql="insert into t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgpath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql="delete from t_book where `id`=?";
        return update(sql,id);
    }

    @Override
    public int update(Book book) {
        String sql="update t_book set`name`=?,`author`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? where `id`=?";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),
                book.getImgpath(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql="select * from t_book  where `id`=?";
        return queryForone(Book.class,sql,id);
    }

    @Override
    public List<Book> queryBook() {
        String sql="select * from t_book";
        return queryForlsit(Book.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from t_book";
        Number count=(Number) queryForSinglevalue(sql);
        return count.intValue();
    }

    @Override
    public List<Book> queryForPageItems(int begin, int pageSize) {
        String sql="select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` as imgpath from t_book limit ?,? ";
        return queryForlsit(Book.class,sql,begin,pageSize);
    }
    @Override
    public Integer queryForPageTotalCount(int min,int max){
        String sql="select count(*) from t_book where price between ? and ?";
        Number count= (Number) queryForSinglevalue(sql, min, max);
        return count.intValue();
    }
    @Override
    public List<Book> queryForPageItems(int min, int max,int begin, int pageSize ) {
        String sql="select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` as imgpath from t_book where price between ? and ? limit ?,? ";
        return queryForlsit(Book.class,sql,min,max,begin,pageSize);
    }
}
